//index.js
//获取应用实例
var app = getApp()
var adds = {};
Page({
  data: {
    date: "",
    array: [],
    index: null,
    hide: false,
    img_arr: [],
    formdata: '',
    data_arr: [],
    radioIndex: "男",
  },
  radio: function(e) {
    this.setData({
      radioIndex: e.detail.value,
    })
  },
  // bindBlur(e){
  //   console.log(e.detail.value)
  //   var str = /13[0123456789]{1}\d{8}|15[0123456789]\d{8}|17[0123456789]\d{8}|18[0123456789]\d{8}/;    
  //   if (str.test(e.detail.value) == false) {
  //     wx.showToast({
  //       title: '手机号码不对',
  //       image:"/hyb_yl/images/err.png"
  //     })
  //   }
  // },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  switchChange: function(e) {
    console.log('switch1 发生 change 事件，携带值为', e.detail.value)
  },
  // uploadImage: function() {
  //   let that = this;
  //   var imgss = that.data.imgs;
  //   wx.chooseImage({
  //     count: 6,
  //     sizeType: ["original", "compressed"],
  //     sourceType: ["album", "camera"],
  //     success: function(res) {
  //       imgss.push(res.tempFilePaths);
  //       that.setData({
  //         imgs: imgss
  //       })
  //       if (imgss.length == 3) {
  //         that.setData({
  //           hide: true
  //         })
  //       }
  //     },
  //   })
  // },
  deleteimg: function(e) {

    var i = e.currentTarget.dataset.index;
    var imgs = this.data.img_arr;
    console.log(imgs)
    imgs.splice(i, 1);
    this.setData({
      img_arr: imgs,
      hide: false
    })
  },
  formSubmit: function(e) {
    var that = this;
    adds = e.detail.value;
    console.log(adds);
    var str = /13[0123456789]{1}\d{8}|15[0123456789]\d{8}|17[0123456789]\d{8}|18[0123456789]\d{8}/;
    if (adds.us_name == "") {
      wx.showToast({
        title: '请输入您的姓名',
        image: "/hyb_yl/images/err.png"
      })
    } else if (str.test(e.detail.value.phone) == false || adds.phone == "") {
      wx.showToast({
        title: '手机号码不对',
        image: "/hyb_yl/images/err.png"
      })
    } else if (adds.title_content == "") {
      wx.showToast({
        title: '请输入您的描述',
        image: "/hyb_yl/images/err.png"
      })
    } else if (adds.shenfen == "") {
      wx.showToast({
        title: '请选择您的身份',
        image: "/hyb_yl/images/err.png"
      })
    } else {
      var values = e.detail.value;
      var us_openid = wx.getStorageSync('openid')
      var us_name = adds.us_name
      var phone = adds.phone
      var title_content = adds.title_content
      var shenfen = adds.shenfen
      var neirong = adds.neirong
      app.util.request({
        url: 'entry/wxapp/Binglikuarr',
        data: {
          us_openid: us_openid,
          us_name: us_name,
          phone: phone,
          title_content: title_content,
          shenfen: shenfen,
          neirong: neirong
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function(res) {
          wx.showModal({
            title: '提交成功',
            content: '',
            showCancel: false,
            success: function(res) {
              wx.reLaunch({
                url: '/hyb_yl/tabBar/index/index',
              })
            }
          });
        },
        fail: function(err) {
          console.log(err);
        }
      })
    }
  },
  //事件处理函数
  onLoad: function(options) {
    var bgc = wx.getStorageSync('color')
    if (options.id) {
      var id = options.id
      app.util.request({
        'url': 'entry/wxapp/Getbingli',
        data: {
          id: id
        },
        success: function(res) {
          console.log(res)
          that.setData({
            rows: res.data,
            hiden: true
          })
        },
      })
    }
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    this.getBase();
    var that = this
    var uniacid = app.siteInfo.uniacid;
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    that.setData({
      uniacid: uniacid
    })
  },
  getBase: function() {
    var that = this;
    console.log(app);
    app.util.request({
      'url': 'entry/wxapp/Base',
      success: function(res) {
        console.log(res)
        that.setData({
          show_title: res.data.data.show_title
        })
      },
      fail: function(err) {
        console.log(err)
      },

    })
  },
  upload: function() {
    var that = this
    var uniacid = that.data.uniacid
    var data_arr = that.data.data_arr;
    var str = /13[0123456789]{1}\d{8}|15[0123456789]\d{8}|17[0123456789]\d{8}|18[0123456789]\d{8}/;
    if (str.test(adds.phone) == false) {
      wx.showToast({
        title: '手机号码不对',
        image: "/hyb_yl/images/err.png"
      })
    } else {

      for (var i = 0; i < this.data.img_arr.length; i++) {
        wx.uploadFile({
          url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=hyb_yl',
          filePath: that.data.img_arr[i],
          name: 'upfile',
          formData: adds,
          success: function(res) {
            console.log(res)
            data_arr.push(res.data)
            that.setData({
              data_arr: data_arr
            })
          }
        })
      }
    }
    this.setData({
      formdata: ''
    })
  },
  upimg: function() {
    var that = this;
    console.log(this.data.img_arr)
    if (this.data.img_arr.length < 3) {
      wx.chooseImage({
        count: 3,
        sizeType: ['original', 'compressed'],
        success: function(res) {
          console.log(res)
          if (res.tempFilePaths.length == 3) {
            that.setData({
              hide: true
            })
          }
          that.setData({
            img_arr: that.data.img_arr.concat(res.tempFilePaths)
          })
        }
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },

})